/*-
 * <<
 * task
 * ==
 * Copyright (C) 2019 sia
 * ==
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * >>
 */

package com.sia.config.web.controller;

import com.sia.config.web.constants.Constants;
import com.sia.config.web.service.LogService;
import com.sia.config.web.util.PageBean;
import com.sia.core.web.vo.ResultBody;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Various query operations for joblog and tasklog
 *
 * @author maozhengwei
 * @version V1.0.0
 * @date 2019-04-28 15:40
 * @see
 */
@RestController
@RequestMapping("/logapi")
@Slf4j
public class LogController {

  @Autowired private LogService logService;

  /**
   * The front end calls the interface Gets the JobLog and associated TaskLog
   *
   * @return JobLog and associated TaskLog pageData
   */
  @RequestMapping(
      value = "/jobAndTaskLogVos",
      method = {RequestMethod.POST, RequestMethod.GET})
  @ResponseBody
  public String selectJobLogAndTaskLogList(
      @RequestParam String jobGroupName,
      @RequestParam String jobKey,
      @RequestParam int currentPage,
      @RequestParam int pageSize) {
    PageBean<?> pageData;
    try {
      pageData = logService.selectJobLogAndTaskLogList(jobGroupName, jobKey, currentPage, pageSize);
    } catch (Exception e) {
      log.error(Constants.LOG_PREFIX + " selectJobLogAndTaskLogList TASK Exception : ", e);
      return ResultBody.error();
    }
    return ResultBody.success(pageData);
  }

  /**
   * Gets the number of logs contained in each group
   *
   * @return the number of logs contained in each group
   */
  @RequestMapping(
      value = "/countGroupsJobLogs",
      method = {RequestMethod.POST, RequestMethod.GET})
  @ResponseBody
  public String selectCountGroupsJobLogs(@RequestParam String jobGroupName) {
    List<Map<String, Integer>> jobLogsByGroup;
    try {
      jobLogsByGroup = logService.selectCountGroupsJobLogs(jobGroupName);
    } catch (Exception e) {
      log.error(Constants.LOG_PREFIX + " selectCountGroupsJobLogs TASK Exception : ", e);
      return ResultBody.error();
    }
    return ResultBody.success(jobLogsByGroup);
  }
}
